package com.fzdy.product.service.impl;

import com.fzdy.product.base.vo.FrontPage;
import com.fzdy.product.controller.api.CopyProcessApi;
import com.fzdy.product.controller.api.OrderApi;
import com.fzdy.product.dao.*;
import com.fzdy.product.entity.Order;
import com.fzdy.product.entity.OrderController;
import com.fzdy.product.entity.OrderProcess;
import com.fzdy.product.entity.Process;
import com.fzdy.product.service.OrderService;
import com.fzdy.product.util.CheckNull;
import com.fzdy.product.util.SQLString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private FinalProductDao finalProductDao;
    @Autowired
    private OrderProductDao orderProductDao;
    @Autowired
    private ProcessResultDao processResultDao;
    @Autowired
    private ProductProcessDao productProcessDao;
    @Autowired
    private OrderControllerDao orderControllerDao;
    @Autowired
    private OrderProcessDao orderProcessDao;
    @Autowired
    private ProcessDao processDao;


    @Override
    public Page getOrdersByPage(FrontPage<OrderApi> page) {
        OrderApi api = page.getSearch();
        Order order = (CheckNull.checkObjNull(api) ? new Order(api.getOrderId(), api.getName(), api.getCustomer(),
                api.getType()) : new Order());
        return orderDao.findAll(Example.of(order), FrontPage.getPage(page));
    }

    @Override
    public boolean checkSnRange(OrderApi api) {
        List<Order> allOrders = orderDao.findAll();
        String beginSN = api.getBeginSN();
        String endSN = beginSN.substring(0, 5) + String.format("%05d", Integer.valueOf(beginSN.substring(5)) + api.getDeviceNum());
        for (Order order : allOrders) {
            Integer count = (Integer) finalProductDao.sqlSingleList("select count(id) from t_" + order.getOrderId() + "_final_product where status = 1 and id between '" + beginSN + "' and '" + endSN + "'").get(0);
            if (count > 0)
                return false;
        }
        return true;
    }

    @Override
    @Transactional
    public boolean insertOrder(OrderApi api) {
        if (orderDao.findFirstByOrderId(api.getOrderId()) == null) {
            return false;
        }
        Order order = new Order(api.getOrderId(), api.getName(), api.getDeviceNum(), api.getBeginSN(), api.getSoftVersion(), api.getFirmwareVersion(), api.getProductType(), api.getProcessCollection(), api.getProcessCollectionDemo(), api.getCustomer());
        // 添加 订单
        Integer orderId = orderDao.save(order).getId();
        // 添加 对应控制器
        for (Integer i : api.getControllerid()) {
            orderControllerDao.save(new OrderController(orderId, i));
        }
        //新建四个表，表名为t_订单名_基表名，如t_abc001_final_product
        finalProductDao.createTable(SQLString.getTFinalProduct(api.getOrderId()), "finalproduct");
        orderProductDao.createTable(SQLString.getTOrderProduct(api.getOrderId()), "orderproduct");
        processResultDao.createTable(SQLString.getTProcessResult(api.getOrderId()), "processresult");
        productProcessDao.createTable(SQLString.getTProductProcess(api.getOrderId()), "productprocess");
        // finalProductDao批量添加客户SN
        finalProductDao.batchSave(api.getBeginSN(), api.getDeviceNum(), SQLString.getTFinalProduct(api.getOrderId()));

        return true;
    }

    @Override
    public Order getOrderByOrderId(String orderId) {
        return orderDao.findFirstByOrderId(orderId);
    }

    @Override
    @Transactional
    public void updateNum(Order order) {
        Long delBeginSn = Long.valueOf(order.getBeginSn()) + order.getNewNum() - 1;
        int delNum = order.getDeviceNum() - order.getNewNum();
        finalProductDao.batchDel(delBeginSn.toString(), delNum, SQLString.getTProductProcess(order.getOrderId()));
        order.setDeviceNum(order.getNewNum());
        orderDao.saveAndFlush(order);
    }

    @Override
    public void copyProcess(CopyProcessApi api) {
        OrderProcess orderProcess = new OrderProcess(api.getCopyProcessId());
        List<OrderProcess> orderProcessList = orderProcessDao.findAll(Example.of(orderProcess));
        List<Process> processeList=processDao.findAllById(orderProcessList.stream().map(OrderProcess::getProcessId).collect(Collectors.toList()));
        for (Process process:processeList) {
            Process newProcess=new Process(process);
            newProcess=processDao.save(newProcess);

        }

    }


}
